package com.example.library.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.library.common.Result;
import com.example.library.entity.Book;
import com.example.library.entity.Bookclass;
import com.example.library.entity.Bookshelf;
import com.example.library.mapper.BookshelfMapper;
import com.example.library.service.IBookService;
import com.example.library.service.IBookclassService;
import com.example.library.service.IBookshelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.geom.RectangularShape;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 胡绍杆
 * @since 2022-06-16
 */
@RestController
@RequestMapping("/bookshelf")
public class BookshelfController {

    @Autowired
    private IBookshelfService bookshelfService;

    @Autowired
    private IBookService bookService;

    @Autowired
    private BookshelfMapper bookshelfMapper;

    @Autowired
    private IBookclassService bookclassService;

    @GetMapping("/page")
    public Result findPage() {
        return Result.success(bookshelfMapper.shelfPage());
    }

    @GetMapping("/getClassName")
    public Result getClassName(){
        return Result.success(bookclassService.list());
    }

    @GetMapping("/changeShelf")
    public Result changeShelf(@RequestParam String shelfNum,
                              @RequestParam Integer shelfState,
                              @RequestParam String floor,
                              @RequestParam String area){
        if (shelfState==0)
        {
            QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
            queryWrapper.like("book_num",shelfNum);
            queryWrapper.ne("book_state",0).ne("book_state",3);
            int count = bookService.count(queryWrapper);
            if (count>0){
                return Result.error("500","禁用失败，书架上还有图书");
            }
        }
        QueryWrapper<Bookshelf> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("shelf_state",1);
        queryWrapper.like("floor",floor).like("area",area);
        int count = bookshelfService.count(queryWrapper);
        if (count==20)
        {
            return Result.error("500","启用失败，该书架所属区域书架数量已满");
        }
        UpdateWrapper<Bookshelf> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("shelf_state",shelfState);
        updateWrapper.eq("shelf_num",shelfNum);
        return Result.success(bookshelfService.update(updateWrapper));
    }

    @GetMapping("/addshelf")
    public Result addShelf(@RequestParam String floor,
                           @RequestParam String area,
                           @RequestParam String className){
        Bookshelf bookshelf=new Bookshelf();
        bookshelf.setFloor(floor);
        bookshelf.setArea(area);
        bookshelf.setShelfState(0);
        QueryWrapper<Bookshelf> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("floor",floor).eq("area",area);
        int count = bookshelfService.count(queryWrapper)+1;
        if (count==100)
        {
            return Result.error("500","该区域书架数量过多无法添加");
        }
        StringBuffer shelf_num=new StringBuffer();
        shelf_num.append(floor);
        shelf_num.append(area);
        shelf_num.append("-");
        String theNum="";
        if (count<10) {
            theNum="0"+count;
        }
        else {
            theNum=Integer.toString(count);
        }

        QueryWrapper<Bookclass> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("class_name",className);
        List<Bookclass> list = bookclassService.list(queryWrapper1);
        bookshelf.setClassId(list.get(0).getClassId());
        shelf_num.append(theNum);
        String shelfNum=shelf_num.toString();
        bookshelf.setShelfNum(shelfNum);
        return Result.success(bookshelfService.save(bookshelf));
    }


    //图书上架
    //根据楼层和区域查找书架
    @GetMapping("/getDetialshelf")
    public Result getDetialshelf(@RequestParam String floor,
                               @RequestParam String area) {
        QueryWrapper<Bookshelf> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("floor", floor);
        queryWrapper.eq("area", area);
        queryWrapper.eq("shelf_state", 1);
        return Result.success(bookshelfService.list(queryWrapper));
    }

    @GetMapping("/getBooks")
    public Result getBooks(@RequestParam String shelfNum){
        QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("book_num",shelfNum);
        return Result.success(bookService.list(queryWrapper));


    }

    @GetMapping("/moveBooks")
    public Result moveBooks(@RequestParam String shelfNum){
        QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("book_num",shelfNum);
        List<Book> list = bookService.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            String nums = list.get(i).getBookNum().substring(5, 9);
            list.get(i).setBookNum("back"+nums);
        }
        for (int i = 0; i < list.size(); i++) {
            bookService.saveOrUpdate(list.get(i));
        }
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("shelf_num",shelfNum);
        updateWrapper.set("shelf_state",0);

        return Result.success(bookshelfService.update(updateWrapper));

    }


}
